﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Web.Mvc;
using RiderShop.Web.YinxinPay;

namespace RiderShop.Mobile.Tests
{
    [TestClass]
    public class UnitTesYinxinPay
    {
        DateTime dt = DateTime.Now;

        //测试卡卡号 & 密码
        //private const string cardNo = "2980021500000000494";
        //private const string cardPwd = "335429";

        //david卡号 & 密码
        //private const string cardNo = "2980021500000002134";
        //private const string cardPwd = "388478";

        //姚尧卡号 & 密码
        private const string cardNo = "2980021500000002143";
        private const string cardPwd = "907826";

        //银信商通内部卡号 & 密码
        //private const string cardNo = "2980020000010918730";
        //private const string cardPwd = "919171";
        
        /// <summary>
        /// 测试查询订单
        /// </summary>
        [TestMethod]
        public void TestQueryOrder()
        {
            string expected = string.Empty;//期望值
            string actual = "123456";//真实值

            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); 

            for (int i = 0; i < 10000; i++)
            {
                //随机生成16位数
                Random rd = new Random();
                int num = rd.Next(100000, 100000000);
                int num2 = rd.Next(100000, 100000000);
                string sujishu =  num.ToString() + num2.ToString();
               
                string order = YinxinPayHelper.CreateGatewayOrderNumber(sujishu);
                //重置计时器
                stopwatch.Reset();
                //计时开始
                stopwatch.Start(); 
                actual = YinxinPayHelper.QueryOrder(dt.ToFileTime().ToString() + i);
                //计时结束
                stopwatch.Stop();

                Assert.AreEqual(expected, actual,"两个不相等，测试失败");
            }
        }

        /// <summary>
        /// 测试查询余额
        /// </summary>
        [TestMethod]
        public void TestQueryBalance()
        {
            string expected = "YES";//期望值
            string actual = "NO";//真实值
            string yuer = "";//余额
            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); 
 
            for (int i = 0; i < 10; i++)
            {
                //重置计时器
                stopwatch.Reset();
                //计时开始
                stopwatch.Start();
                actual = YinxinPayHelper.QueryBalance(cardNo, cardPwd);
                //计时结束
                stopwatch.Stop();

                if(actual.IndexOf("￥") == 0)
                {
                    yuer = actual;
                    actual = "YES";
                }

                Console.WriteLine("ticks: " + stopwatch.ElapsedTicks +
                " ms: " + stopwatch.ElapsedMilliseconds + " yuer: " + yuer);
                Assert.AreEqual(expected, actual, "两个不相等，查询余额测试失败");
            }
        }

        /// <summary>
        /// 测试提交订单
        /// </summary>
        [TestMethod]
        public void TestCommitOrder()
        {
            string expected = string.Empty;//期望值
            string actual = "123456";//真实值

            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();

            for (int i = 0; i < 1; i++)
            {
                //随机生成12位数
                Random rd = new Random();
                int num = rd.Next(100000, 1000000);
                int num2 = rd.Next(100000, 1000000);
                string sujishu = num.ToString() + num2.ToString();

                string order = YinxinPayHelper.CreateGatewayOrderNumber(sujishu);
                //重置计时器
                stopwatch.Reset();
                //计时开始
                stopwatch.Start();
                actual = YinxinPayHelper.CommitOrder(order, cardNo, cardPwd, 0.01M);
                //计时结束
                stopwatch.Stop();

                Console.WriteLine("ticks: " + stopwatch.ElapsedTicks +
               " ms: " + stopwatch.ElapsedMilliseconds);
                Assert.AreEqual(expected, actual, "两个不相等，提交订单测试失败");
            }
        }

        /// <summary>
        /// 测试查询余额 & 提交订单(整套流程)
        /// </summary>
        [TestMethod]
        public void TestQueryBalanceAndCommitOrder()
        {
            //查询余额的期望值和实际值
            string expected_QueryBalance = "YES";
            string actual_QueryBalance =  "NO";;
            string yuer = "";//卡里余额

            //提交订单的期望值和实际值
            string expected_CommitOrder = string.Empty;
            string actual_CommitOrder = "123456";

            //查询订单的期望值和实际值
            string expected_QueryOrder = string.Empty;
            string actual_QueryOrder = "123456";

            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); 

            for (int i = 0; i < 200; i++)
            {               
                //随机生成12位数
                Random rd = new Random();
                int num = rd.Next(100000, 1000000);
                int num2 = rd.Next(100000, 1000000);
                string sujishu = num.ToString() + num2.ToString();

                //重置计时器
                stopwatch.Reset();
                //计时开始
                stopwatch.Start();

                //1.查询余额
                actual_QueryBalance = YinxinPayHelper.QueryBalance(cardNo, cardPwd);
                if (actual_QueryBalance.IndexOf("￥") == 0)
                {
                    yuer = actual_QueryBalance;//余额
                    actual_QueryBalance = "YES";
                }
                Console.WriteLine("1 step 未付款余额: " + yuer);
                Assert.AreEqual(expected_QueryBalance, actual_QueryBalance, "1 step 查询余额测试失败");

                //2.提交订单支付
                string order = YinxinPayHelper.CreateGatewayOrderNumber(sujishu);
                actual_CommitOrder = YinxinPayHelper.CommitOrder(order, cardNo, cardPwd, 0.01M);
                Console.WriteLine("2 step 提交订单: " + order);
                Assert.AreEqual(expected_CommitOrder, actual_CommitOrder, "2 step 提交订单测试失败");


                //3.支付完查询余额
                actual_QueryBalance = YinxinPayHelper.QueryBalance(cardNo, cardPwd);
                if (actual_QueryBalance.IndexOf("￥") == 0)
                {
                    yuer = actual_QueryBalance;//余额
                    actual_QueryBalance = "YES";
                }
                Console.WriteLine("3 step 支付完余额: " + yuer);
                Assert.AreEqual(expected_QueryBalance, actual_QueryBalance, "3 step 支付完查询余额测试失败");

                //4.查询订单
                actual_QueryOrder = YinxinPayHelper.QueryOrder(order);
                Console.WriteLine("4 step 查询订单: " + order);
                Assert.AreEqual(expected_QueryOrder, actual_QueryOrder, "4 step 查询订单测试失败");

                //计时结束
                stopwatch.Stop();

                Console.WriteLine("ticks: " + stopwatch.ElapsedTicks +
                 " ms: " + stopwatch.ElapsedMilliseconds);
            }
        }       
    }
}
